package com.edu.service.impl;

import com.edu.commons.ServiceCode;
import com.edu.commons.ServiceException;
import com.edu.mapper.ProductMapper;
import com.edu.pojo.Product;
import com.edu.service.ProductService;
import com.edu.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: myApp
 * @Titile: ProductServiceImpl
 * @Author: Administrator
 * @Description: 商品业务实现类
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<ProductVO> getProductList() {
        List<Product> products = productMapper.findAll();
        //为了演示数据为空，设置空集合
        //List<Product> products = new ArrayList<>();
        List<ProductVO> productVOList = new ArrayList<>();
        if(products == null || products.isEmpty()){
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"商品查询失败：没有商品数据");
        }
        //todo 可以使用对象的拷贝（复制）插件
        products.forEach(product -> {
            productVOList.add(new ProductVO(product.getProductId(),product.getProductName(),
                    product.getProductPrice(), product.getProductNum()));
        });
        return productVOList;
    }
}
